using System.Collections.Generic;
using System.Linq;
using Dimebrain.TweetSharp.Core.Extensions;
using visualtwimilar.Algorithms;

namespace visualtwimilar
{
  public class DataInitialyzer
  {
    private readonly double _height;
    private readonly MainViewModel _model;
    private readonly IDictionary<string, IDictionary<string, double>> _prefs;
    private readonly string _userName;
    private readonly double _width;

    public DataInitialyzer(MainViewModel model, string userName, double width, double height)
    {
      _model = model;
      _userName = userName;
      _width = width;
      _height = height;
      _prefs = new Dictionary<string, IDictionary<string, double>>();
    }

    public void Initialyze()
    {
      _prefs.LoadForUser(_userName);
      _userName.GetFriends().Take(50).ToList().Map(x => _prefs.LoadForUser(x));

      _prefs[_userName].Keys.ToList()
        .ForEach(word => word.GetUsersWhoTwitThis()
                           .Map(x => _prefs.LoadForUser(x)));
      LoadUsers();
    }

    private void LoadUsers()
    {
      var shapes = Clusters.ScaleDown<object>(_prefs, _width, _height);
      foreach (var shape in shapes)
      {
        _model.AddUser(shape.Key, double.Parse(shape.Value[0].ToString()),
                      double.Parse(shape.Value[1].ToString()));
      }
    }
  }
}